from django.db import models

# Create your models here.
TYPE_CHOICE = (
    (1, '物理机'),
    (2, '刀片机'),
    (3, '云主机'),
    (4, '虚拟机'),
)

# 参考阿里云服务器信息设计的数据库表：https://help.aliyun.com/document_detail/25506.htm?spm=a2c4g.11186623.2.38.4a412270WIYZnz#t9865.html
# 一台云服务器的用户和密码账号可以是多个
# Machine-Account:1:N
class Machine(models.Model):
    InstanceId = models.CharField(verbose_name='实例Id', max_length=200)
    Cpu = models.IntegerField(verbose_name='CPU数', help_text='CPU数')
    CreationTime = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    ExpiredTime = models.DateTimeField(verbose_name="过期时间", null=True, blank=True)
    # null-跟数据库有关， blank-和数据验证（表单验证等）有关
    Description = models.CharField(verbose_name='描述', max_length=200, null=True, blank=True)
    HostName = models.CharField(verbose_name="主机名", max_length=200, null=True, blank=True)
    InnerIpAddress = models.JSONField(verbose_name="内网IP", default=[],  null=True, blank=True)
    PublicIpAddresses = models.JSONField(verbose_name="公网IP", default=[],  null=True, blank=True)
    Memory = models.IntegerField(verbose_name='内存大小', help_text='内存大小(单位为G)')
    OSName = models.CharField(verbose_name="操作系统名称", max_length=200, null=True, blank=True)
    OSType = models.IntegerField(verbose_name="操作系统类型", choices=((1, 'windows'), (2, 'linux')))
    SerialNumber = models.CharField(verbose_name="序列号", max_length=200)
    Status = models.CharField(verbose_name="实例状态", max_length=20, choices=(('Running', "运行中"), ('Stopped', "已关机")))
    MachineType = models.IntegerField(verbose_name='服务器类型', choices=TYPE_CHOICE, default=4)
    CreateMethod = models.IntegerField(verbose_name="添加方式", choices=((1, 'input'), (2, 'sdk')), default=1)

    def __str__(self):
        return self.SerialNumber

    class Meta:
        verbose_name = '服务器管理'
        verbose_name_plural = verbose_name


class Account(models.Model):
    User = models.CharField(verbose_name='用户名', max_length=20, null=True, blank=True)
    Password = models.CharField(verbose_name='密码', max_length=200, null=True, blank=True)
    Private_key = models.CharField(verbose_name="私钥文件名", max_length=200, null=True, blank=True)
    Machine = models.ForeignKey(Machine, related_name='accounts', on_delete=models.CASCADE)

    def __str__(self):
        return self.User

    class Meta:
        verbose_name = '服务器账户管理'
        verbose_name_plural = verbose_name
